package com.gitee.feizns.quickstart.web.decrypt;

import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * 参数解密器
 * @see HandlerMethodArgumentResolver
 * @author feizns
 * @since 2023/2/4
 */
public class DecryptParameterResolver implements HandlerMethodArgumentResolver {

    private final DecryptionAlgorithm algorithm;

    public DecryptParameterResolver(DecryptionAlgorithm algorithm) {
        this.algorithm = algorithm;
    }

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.hasParameterAnnotation(Decrypt.class);
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        return algorithm.decrypt(webRequest.getParameter(parameter.getParameter().getName()));
    }

}
